home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
cpphtp2
/
code.jar
/
code
/
ch21
/
fig21_04.txt
< prev
next >
Wrap
Text File
|
1998-02-27
|
831b
|
28 lines
1 // Fig. 21.4: fig21_04.cpp
2 // Demonstrating reinterpret_cast operator.
3 #include <iostream.h>
4
5 int main()
6 {
7 unsigned x = 22, *unsignedPtr;
8 void *voidPtr = &x;
9 char *charPtr = "C++";
10
11 // cast from void * to unsigned *
12 unsignedPtr = reinterpret_cast< unsigned * >( voidPtr );
13
14 cout << "*unsignedPtr is " << *unsignedPtr
15 << "\ncharPtr is " << charPtr;
16
17 // use reinterpret_cast to cast a char * pointer to unsigned
18 cout << "\nchar * to unsigned results in: "
19 << ( x = reinterpret_cast< unsigned >( charPtr ) );
20
21 // cast unsigned back to char *
22 cout << "\nunsigned to char * results in: "
23 << reinterpret_cast< char * >( x ) << endl;
24
25 return 0;
26 }
}